package cn.edu.fzu.delivery.controller.sys;

import cn.edu.fzu.delivery.common.PageVO;
import cn.edu.fzu.delivery.common.Result;
import cn.edu.fzu.delivery.domain.entity.NetWorkDay;
import cn.edu.fzu.delivery.domain.entity.SysUser;
import cn.edu.fzu.delivery.service.NetworkService;
import cn.edu.fzu.delivery.session.SessionConstant;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.oas.annotations.EnableOpenApi;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @author yifan
 * @date 2022/11/14
 */
@EnableOpenApi
@RestController
@RequestMapping("/api/auth/sys/networkDay")
@Api(tags = "系统管理员-网点路线管理")
public class NetworkDayController {

    @Resource
    NetworkService networkService;


    @GetMapping("/list")
    @ApiOperation(value = "获取网点路线列表", httpMethod = "GET")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "networkId1", value = "网点id1"),
            @ApiImplicitParam(name = "networkId2", value = "网点id2"),
            @ApiImplicitParam(name = "day", value = "路线耗时（单位：天）"),
            @ApiImplicitParam(name = "pageNum", required = true),
            @ApiImplicitParam(name = "pageSize", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result list(Long networkId1, Long networkId2, Integer day, Integer pageNum, Integer pageSize,
                       HttpServletRequest request) {
        try (Page<NetWorkDay> page = PageHelper.startPage(pageNum, pageSize)) {
            networkService.listNetWorkDay(networkId1, networkId2, day);
            return new Result(new PageVO<>(page.getTotal(), page.getResult()));
        }
    }

    @PostMapping("/add")
    @ApiOperation(value = "添加网点路线", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "networkId1", value = "网点id1", required = true),
            @ApiImplicitParam(name = "networkId2", value = "网点id2", required = true),
            @ApiImplicitParam(name = "day", value = "路线耗时（单位：天）", required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result add(Long networkId1, Long networkId2, Integer day, HttpServletRequest request) {
        networkService.addNetworkDay(networkId1, networkId2, day);
        return new Result("添加成功！");
    }


    @PostMapping("/delete")
    @ApiOperation(value = "删除网点路线", httpMethod = "POST")
    @ApiImplicitParams({ @ApiImplicitParam(name = "id", value = "网点路线id",  required = true) })
    @ApiResponse(code = 200, message = "", response = Result.class)
    public Result delete(Long id, HttpServletRequest request) {
        SysUser currentUser = (SysUser) request.getSession().getAttribute(SessionConstant.DELIVERY_ACCOUNT);
        networkService.deleteNetworkDay(id, currentUser);
        return new Result("删除成功！");
    }


    @PostMapping("/update")
    @ApiOperation(value = "修改网点路线", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "网点路线id", required = true),
            @ApiImplicitParam(name = "networkId1", value = "网点id1", required = true),
            @ApiImplicitParam(name = "networkId2", value = "网点id2", required = true),
            @ApiImplicitParam(name = "day", value = "路线耗时（单位：天）", required = true)
    })
    public Result update(Long id, Long networkId1, Long networkId2, Integer day) {
        networkService.updateNetworkDay(id, networkId1, networkId2, day);
        return new Result("修改成功！");
    }
}
